// var $resource = require("./AL_Framewrok_Resource");
// var n = (function () {
//     function e() {
//         this._resources = new Map();
//         this._loadedCount = 0;
//         this._loadedResource = new Map();
//         this._isLoading = !1;
//         this._tag = null;
//     }
//     Object.defineProperty(e.prototype, "tag", {
//         get: function () {
//             return this._tag;
//         },
//         set: function (e) {
//             this._tag = e;
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     Object.defineProperty(e.prototype, "chuli_ziyuan_jiazai_wancheng", {
//         get: function () {
//             return this._chuli_ziyuan_jiazai_wancheng;
//         },
//         set: function (e) {
//             this._chuli_ziyuan_jiazai_wancheng = e;
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     Object.defineProperty(e.prototype, "chuli_ziyuan_jiazai_jindu", {
//         get: function () {
//             return this._chuli_ziyuan_jiazai_jindu;
//         },
//         set: function (e) {
//             this._chuli_ziyuan_jiazai_jindu = e;
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     Object.defineProperty(e.prototype, "chuli_ziyuan_jiazai_huoqu", {
//         get: function () {
//             return this._chuli_ziyuan_jiazai_huoqu;
//         },
//         set: function (e) {
//             this._chuli_ziyuan_jiazai_huoqu = e;
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     e.prototype.jiazai_ziyuan = function () {
//         var e = this;
//         if (this.chuli_ziyuan_jiazai_huoqu) {
//             var t = this.chuli_ziyuan_jiazai_huoqu();
//             if (t) {
//                 if (t.length <= 0) {
//                     if (this.chuli_ziyuan_jiazai_wancheng) {
//                         this.chuli_ziyuan_jiazai_wancheng($resource.AL_Framewrok_Resource.LoaderError.NO_FOUND_LOAD_RESOURCE);
//                     }
//                 } else {
//                     if (this._isLoading) {
//                         if (this.chuli_ziyuan_jiazai_wancheng) {
//                             this.chuli_ziyuan_jiazai_wancheng($resource.AL_Framewrok_Resource.LoaderError.LOADING);
//                         }
//                     } else {
//                         {
//                             if (this._resources.size > 0 && this.panduan_ziyuan_shifou_jiazai_wancheng()) {
//                                 if (this.chuli_ziyuan_jiazai_wancheng) {
//                                     this.chuli_ziyuan_jiazai_wancheng($resource.AL_Framewrok_Resource.LoaderError.SUCCESS);
//                                 }
//                                 return void this.chuli_ziyuan_jiazai_wancheng_huidiao();
//                             }
//                             this._isLoading = !0;
//                             t.forEach(function (t) {
//                                 t.url ? e._resources.set(t.url, t) : t.dir && e._resources.set(t.dir, t);
//                             });
//                             this._loadedCount = 0;
//                             this._resources.forEach(function (t) {
//                                 t.dir
//                                     ? Manager.assetManager.loadDir({
//                                           bundle: t.bundle,
//                                           path: t.dir,
//                                           type: t.type,
//                                           onComplete: e._chuli_ziyuan_jiazai_wancheng_huidiao.bind(e),
//                                           shifou_yanchi_shifang_ziyuan: t.shifou_yanchi_shifang_ziyuan
//                                       })
//                                     : Manager.assetManager.load({
//                                           bundle: t.bundle,
//                                           path: t.url,
//                                           type: t.type,
//                                           onComplete: e._chuli_ziyuan_jiazai_wancheng_huidiao.bind(e),
//                                           shifou_yanchi_shifang_ziyuan: t.shifou_yanchi_shifang_ziyuan
//                                       });
//                             });
//                         }
//                     }
//                 }
//             } else {
//                 if (this.chuli_ziyuan_jiazai_wancheng) {
//                     this.chuli_ziyuan_jiazai_wancheng($resource.AL_Framewrok_Resource.LoaderError.NO_FOUND_LOAD_RESOURCE);
//                 }
//             }
//         } else {
//             if (this.chuli_ziyuan_jiazai_wancheng) {
//                 this.chuli_ziyuan_jiazai_wancheng($resource.AL_Framewrok_Resource.LoaderError.NO_FOUND_LOAD_RESOURCE);
//             }
//         }
//     };
//     e.prototype.xiezai_ziyuan = function () {
//         this._xiezai_ziyuan();
//     };
//     e.prototype._xiezai_ziyuan = function () {
//         var e = this;
//         this._isLoading || this._resources.size <= 0
//             ? this._isLoading && Log.d("resources is loading , waiting for unload!!!")
//             : (this._resources.size > 0 &&
//                   this._resources.forEach(function (t) {
//                       if (t.url) {
//                           if (e._loadedResource.has(t.url)) {
//                               if ((o = e._loadedResource.get(t.url))) {
//                                   Manager.assetManager.releaseAsset(o);
//                               }
//                               e._loadedResource.delete(t.url);
//                           }
//                       } else {
//                           if (t.dir) {
//                               var o;
//                               if (e._loadedResource.has(t.dir)) {
//                                   if ((o = e._loadedResource.get(t.dir))) {
//                                       Manager.assetManager.releaseAsset(o);
//                                   }
//                                   e._loadedResource.delete(t.dir);
//                               }
//                           }
//                       }
//                   }),
//               (this._isLoading = !1),
//               (this._loadedCount = 0),
//               this._resources.clear());
//     };
//     e.prototype._chuli_ziyuan_jiazai_wancheng_huidiao = function (e) {
//         this._loadedCount++;
//         if (this._chuli_ziyuan_jiazai_jindu) {
//             if (this._loadedCount > this._resources.size) {
//                 this._loadedCount = this._resources.size;
//             }
//             this._chuli_ziyuan_jiazai_jindu(this._loadedCount, this._resources.size, e);
//         }
//         if (e && (Array.isArray(e.data) || e.data instanceof cc.Asset)) {
//             var t = new $resource.AL_Framewrok_Resource.Info();
//             t.url = e.info.url;
//             t.type = e.info.type;
//             t.data = e.data;
//             t.bundle = e.info.bundle;
//             t.shifou_yanchi_shifang_ziyuan = e.info.shifou_yanchi_shifang_ziyuan;
//             Manager.assetManager.retainAsset(t);
//             this._loadedResource.set(t.url, t);
//         }
//         this.jiancha_ziyuan_shifou_jiazai_wancheng();
//     };
//     e.prototype.jiancha_ziyuan_shifou_jiazai_wancheng = function () {
//         if (this.panduan_ziyuan_shifou_jiazai_wancheng()) {
//             this._isLoading = !1;
//             if (this.chuli_ziyuan_jiazai_wancheng) {
//                 this.chuli_ziyuan_jiazai_wancheng($resource.AL_Framewrok_Resource.LoaderError.SUCCESS);
//             }
//             this.chuli_ziyuan_jiazai_wancheng_huidiao();
//         }
//     };
//     e.prototype.chuli_ziyuan_jiazai_wancheng_huidiao = function () {};
//     e.prototype.panduan_ziyuan_shifou_jiazai_wancheng = function () {
//         return this._loadedCount >= this._resources.size;
//     };
//     return e;
// })();
// exports.default = n;


const $resource = require("./AL_Framewrok_Resource");

/**
 * 资源管理类，用于加载和卸载资源
 */
class ResourceManager {
    constructor() {
        this._resources = new Map(); // 存储待加载资源的Map
        this._loadedCount = 0; // 已加载资源数量
        this._loadedResource = new Map(); // 已加载资源的Map
        this._isLoading = false; // 是否正在加载资源
        this._tag = null; // 资源标签
    }

    /** 获取或设置资源标签 */
    get tag() {
        return this._tag;
    }

    set tag(value) {
        this._tag = value;
    }

    /** 获取或设置加载完成回调 */
    get chuli_ziyuan_jiazai_wancheng() {
        return this._chuli_ziyuan_jiazai_wancheng;
    }

    set chuli_ziyuan_jiazai_wancheng(callback) {
        this._chuli_ziyuan_jiazai_wancheng = callback;
    }

    /** 获取或设置加载进度回调 */
    get chuli_ziyuan_jiazai_jindu() {
        return this._chuli_ziyuan_jiazai_jindu;
    }

    set chuli_ziyuan_jiazai_jindu(callback) {
        this._chuli_ziyuan_jiazai_jindu = callback;
    }

    /** 获取或设置获取加载资源的函数 */
    get chuli_ziyuan_jiazai_huoqu() {
        return this._chuli_ziyuan_jiazai_huoqu;
    }

    set chuli_ziyuan_jiazai_huoqu(callback) {
        this._chuli_ziyuan_jiazai_huoqu = callback;
    }

    /** 加载资源 */
    jiazai_ziyuan() {
        if (this.chuli_ziyuan_jiazai_huoqu) {
            const resources = this.chuli_ziyuan_jiazai_huoqu();
            if (resources) {
                if (resources.length <= 0) {
                    this.chuli_ziyuan_jiazai_wancheng?.($resource.AL_Framewrok_Resource.LoaderError.NO_FOUND_LOAD_RESOURCE);
                } else if (this._isLoading) {
                    this.chuli_ziyuan_jiazai_wancheng?.($resource.AL_Framewrok_Resource.LoaderError.LOADING);
                } else {
                    if (this._resources.size > 0 && this.panduan_ziyuan_shifou_jiazai_wancheng()) {
                        this.chuli_ziyuan_jiazai_wancheng?.($resource.AL_Framewrok_Resource.LoaderError.SUCCESS);
                        this.chuli_ziyuan_jiazai_wancheng_huidiao();
                        return;
                    }
                    this._isLoading = true;
                    resources.forEach(resource => {
                        if (resource.url) {
                            this._resources.set(resource.url, resource);
                        } else if (resource.dir) {
                            this._resources.set(resource.dir, resource);
                        }
                    });
                    this._loadedCount = 0;
                    this._resources.forEach(resource => {
                        if (resource.dir) {
                            Manager.assetManager.loadDir({
                                bundle: resource.bundle,
                                path: resource.dir,
                                type: resource.type,
                                onComplete: this._chuli_ziyuan_jiazai_wancheng_huidiao.bind(this),
                                shifou_yanchi_shifang_ziyuan: resource.shifou_yanchi_shifang_ziyuan
                            });
                        } else {
                            Manager.assetManager.load({
                                bundle: resource.bundle,
                                path: resource.url,
                                type: resource.type,
                                onComplete: this._chuli_ziyuan_jiazai_wancheng_huidiao.bind(this),
                                shifou_yanchi_shifang_ziyuan: resource.shifou_yanchi_shifang_ziyuan
                            });
                        }
                    });
                }
            } else {
                this.chuli_ziyuan_jiazai_wancheng?.($resource.AL_Framewrok_Resource.LoaderError.NO_FOUND_LOAD_RESOURCE);
            }
        } else {
            this.chuli_ziyuan_jiazai_wancheng?.($resource.AL_Framewrok_Resource.LoaderError.NO_FOUND_LOAD_RESOURCE);
        }
    }

    /** 卸载资源 */
    xiezai_ziyuan() {
        this._xiezai_ziyuan();
    }

    /** 内部卸载资源的实现 */
    _xiezai_ziyuan() {
        if (!this._isLoading && this._resources.size > 0) {
            this._resources.forEach(resource => {
                if (resource.url) {
                    if (this._loadedResource.has(resource.url)) {
                        const loadedAsset = this._loadedResource.get(resource.url);
                        Manager.assetManager.releaseAsset(loadedAsset);
                        this._loadedResource.delete(resource.url);
                    }
                } else if (resource.dir) {
                    if (this._loadedResource.has(resource.dir)) {
                        const loadedAsset = this._loadedResource.get(resource.dir);
                        Manager.assetManager.releaseAsset(loadedAsset);
                        this._loadedResource.delete(resource.dir);
                    }
                }
            });
            this._isLoading = false;
            this._loadedCount = 0;
            this._resources.clear();
        } else if (this._isLoading) {
            Log.d("resources is loading , waiting for unload!!!");
        }
    }

    /** 资源加载完成回调 */
    _chuli_ziyuan_jiazai_wancheng_huidiao(loadedData) {
        this._loadedCount++;

        if (this._chuli_ziyuan_jiazai_jindu) {
            if (this._loadedCount > this._resources.size) {
                this._loadedCount = this._resources.size;
            }
            this._chuli_ziyuan_jiazai_jindu(this._loadedCount, this._resources.size, loadedData);
        }

        if (loadedData && (Array.isArray(loadedData.data) || loadedData.data instanceof cc.Asset)) {
            const resourceInfo = new $resource.AL_Framewrok_Resource.Info();
            resourceInfo.url = loadedData.info.url;
            resourceInfo.type = loadedData.info.type;
            resourceInfo.data = loadedData.data;
            resourceInfo.bundle = loadedData.info.bundle;
            resourceInfo.shifou_yanchi_shifang_ziyuan = loadedData.info.shifou_yanchi_shifang_ziyuan;
            Manager.assetManager.retainAsset(resourceInfo);
            this._loadedResource.set(resourceInfo.url, resourceInfo);
        }

        this.jiancha_ziyuan_shifou_jiazai_wancheng();
    }

    /** 检查资源是否加载完成 */
    jiancha_ziyuan_shifou_jiazai_wancheng() {
        if (this.panduan_ziyuan_shifou_jiazai_wancheng()) {
            this._isLoading = false;
            this.chuli_ziyuan_jiazai_wancheng?.($resource.AL_Framewrok_Resource.LoaderError.SUCCESS);
            this.chuli_ziyuan_jiazai_wancheng_huidiao();
        }
    }

    /** 资源加载完成的处理（空函数，可重写） */
    chuli_ziyuan_jiazai_wancheng_huidiao() {}

    /** 判断资源是否加载完成 */
    panduan_ziyuan_shifou_jiazai_wancheng() {
        return this._loadedCount >= this._resources.size;
    }
}

exports.default = ResourceManager;
